package javamobile.billsplit;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javamobile.utils.MySocket;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;

public class HomeActivity extends Activity {
	public static final String PREFS_NAME = "MyPrefsFile";
    Button friendButton; 
    Button createBillButton; 
    Button pendingBillsButton; 
    Button closedBillsButton; 
    
    TableLayout oweTable;    
    TableLayout waitingTable;  
    TableLayout pastTable;  
  
    View.OnClickListener friendHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(HomeActivity.this, FriendsActivity.class));   
    	}
    };  
    View.OnClickListener createBillHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(HomeActivity.this, CreateBillActivity.class)); 
    	}
    };  
    View.OnClickListener pendingBillsHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(HomeActivity.this, PendingBillsActivity.class)); 
    	}
    };  
    View.OnClickListener closedBillsHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(HomeActivity.this, ClosedBillsActivity.class)); 
    	}
    }; 
    
    @SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home); 
        friendButton = (Button) findViewById(R.id.friend_button_HOME); 
        createBillButton = (Button) findViewById(R.id.create_bill_button_HOME); 
        pendingBillsButton = (Button) findViewById(R.id.pending_bills_button_HOME); 
        closedBillsButton = (Button) findViewById(R.id.closed_bills_button_HOME); 
        friendButton.setOnClickListener(friendHandler); 
        createBillButton.setOnClickListener(createBillHandler);   
        pendingBillsButton.setOnClickListener(pendingBillsHandler); 
        closedBillsButton.setOnClickListener(closedBillsHandler); 
        
        Map<String, Object> input = new HashMap<String, Object>(); 
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String username = settings.getString("username", "DEFAULT");
        input.put("username", username); 
        input.put("action",  "home.do");  
        Map<String, Object> output;    
        Map<Integer, List<Object>> oweList = null; 
        Map<Integer, List<Object>> collectList = null; 
        List<ArrayList<Object>> historyList = null; 
        try {
        	output = MySocket.runSocket(input);  
        	List<String> errors = (List<String>) output.get("errors");
    	    if (errors.size()!=0) {
    	    	Intent toError = new Intent(this, ErrorActivity.class);
    	    	toError.putStringArrayListExtra("errorlist", new ArrayList<String>(errors)); 
    	    	startActivity(toError); 
    	    	finish(); 
    	    } else {
    	    	oweList = (Map<Integer, List<Object>>)output.get("owe");   
    	    	collectList = (Map<Integer, List<Object>>)output.get("collect"); 
    	    	historyList = (List<ArrayList<Object>>)output.get("history");   
    	    }
        } catch (UnknownHostException e) {
			System.out.println("UnknownHostException");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IOException");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			System.out.println("ClassNotFoundException");
			e.printStackTrace();
		}
        
        oweTable = (TableLayout) findViewById(R.id.owed_table_HOME); 
        for(int key: oweList.keySet()) {
        	String nameStr = (String) oweList.get(key).get(0); 
        	String amountStr = ((Double)oweList.get(key).get(1)).toString(); 
        	TextView name = new TextView(this);  
        	name.setText(nameStr); 
        	TextView amount = new TextView(this);  
        	amount.setText(amountStr); 
        	TableRow tr = new TableRow(this);        
        	tr.addView(name); 
        	tr.addView(amount); 
        	oweTable.addView(tr); 
        }
        
        waitingTable = (TableLayout) findViewById(R.id.waiting_to_receive_table_HOME); 
        for(int key: collectList.keySet()) {
        	String nameStr = (String) collectList.get(key).get(0); 
        	String amountStr = ((Double)collectList.get(key).get(1)).toString(); 
        	TextView name = new TextView(this);  
        	name.setText(nameStr); 
        	TextView amount = new TextView(this);  
        	amount.setText(amountStr); 
        	TableRow tr = new TableRow(this);        
        	tr.addView(name); 
        	tr.addView(amount); 
        	waitingTable.addView(tr); 
        }
        
        pastTable = (TableLayout) findViewById(R.id.past_transactions_table_HOME); 
        for(ArrayList<Object> rowList: historyList) {
        	String monthStr = (String) rowList.get(0); 
        	String dueStr = ((Double)rowList.get(1)).toString(); 
        	String collectedStr = ((Double)rowList.get(2)).toString(); 
        	
        	TextView month = new TextView(this);  
        	month.setText(monthStr);  
        	TextView due = new TextView(this);  
        	due.setText(dueStr);  
        	TextView collected = new TextView(this);  
        	collected.setText(collectedStr); 
        	
        	TableRow tr = new TableRow(this);        
        	tr.addView(month); 
        	tr.addView(due); 
        	tr.addView(collected); 
        	tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,   
                LayoutParams.WRAP_CONTENT));       
        	pastTable.addView(tr); 
        } 
    }
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.home, menu); 
        return true; 
    }
    
   /* @Override
    public void onOptionsItemSelected() {
    
    }*/
    

}
